package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import com.sanqing.main.MyDocument;
import com.sanqing.model.Reader;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;


public class ReaderEditIFrame extends JInternalFrame {
    Reader reader;
    JTextField ReadId = new JTextField(13); //读者编号
    JComboBox idType; //证件类别
    JTextField IdentityCard = new JTextField(20); //证件号
    JTextField nameText = new JTextField(20); //读者姓名
    JTextField sexText = new JTextField(20); //读者性别
    JTextField ageText = new JTextField(20); //读者年龄
    //JTextField zyText = new JTextField(20); //读者职业
    JTextField telText = new JTextField(20); //联系电话
    JTextField KeepMoneyText = new JTextField(20); //押金


    public ReaderEditIFrame() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));
        ReadId.setFont(new Font("宋体", Font.PLAIN, 25));
        IdentityCard.setFont(new Font("宋体", Font.PLAIN, 25));
        nameText.setFont(new Font("宋体", Font.PLAIN, 25));
        sexText.setFont(new Font("宋体", Font.PLAIN, 25));
        ageText.setFont(new Font("宋体", Font.PLAIN, 25));
        //zyText.setFont(new Font("宋体", Font.PLAIN, 25));
        telText.setFont(new Font("宋体", Font.PLAIN, 25));
        KeepMoneyText.setFont(new Font("宋体", Font.PLAIN, 25));

        final BorderLayout borderLayout = new BorderLayout();           //边框布局管理器
        getContentPane().setLayout(borderLayout);           //设置布局
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setTitle("修改读者信息");                 //设置窗体标题
        setBounds(100, 100, 800, 350); //设置窗体位置和大小
        final JPanel mainPanel = new JPanel();          //创建中心面板
        mainPanel.setBorder(new EmptyBorder(5, 0, 170, 0));    //设置边框
        final GridLayout gridLayout = new GridLayout(3, 6);  //创建表格布局管理器
        gridLayout.setVgap(2);          //设置组件之间的垂直距离
        gridLayout.setHgap(2);          //设置组件之间的平行距离
        mainPanel.setLayout(gridLayout);
        getContentPane().add(mainPanel);

        reader = new Reader();
        /*long totalMilliSeconds = System.currentTimeMillis();//使用时间戳生成读者证号
        long totalSeconds = totalMilliSeconds / 1000;
        reader.setReaderid(String.valueOf(totalSeconds));*/
        final JLabel ReadIdLabel = new JLabel("读者证号:");
        ReadIdLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        ReadIdLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(ReadIdLabel);
        //创建廿号文本框 "
        ReadId.setDocument(new MyDocument(13));                //书号文本框最大输入值为 13 ReadId.setColumns(13);//设蜜文本框长度
        //ReadId.addKeyListener(new ReadIdkeyListener());                       //注册监听器 ReadId.addFocusListener(new ReadIdFocusListener。);//注册监听器
        mainPanel.add(ReadId);
        final JLabel idTypeLabel = new JLabel("证件类别:");    //创建书籍类别标签
        idTypeLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        idTypeLabel.setHorizontalAlignment(SwingConstants.CENTER);  //对齐方式 idTypeLabeLsetText ("类别：")；	//设置标签文本
        mainPanel.add(idTypeLabel);    //添加到中心面板

        readerTypeModel[] readerTypeModels = new readerTypeModel[3];
        readerTypeModels[0] = new readerTypeModel("身份证", 0);
        readerTypeModels[1] = new readerTypeModel("护照", 1);
        readerTypeModels[2] = new readerTypeModel("学生证", 2);
        AdminComBoxMode adminComBoxMode = new AdminComBoxMode();
        adminComBoxMode.setA(readerTypeModels);


        idType = new JComboBox(adminComBoxMode);
        idType.setFont(new Font("宋体", Font.PLAIN, 25));
        idType.addItemListener(new IdTypeAction());
        mainPanel.add(idType);

        JLabel IdentityCardLabel = new JLabel("证件号:");
        IdentityCardLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        IdentityCardLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //IdentityCard.addActionListener();
        mainPanel.add(IdentityCardLabel);
        mainPanel.add(IdentityCard);

        JLabel nameLabel = new JLabel("姓名:");
        nameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        nameLabel.setHorizontalAlignment(SwingConstants.CENTER);
        //nameText.addActionListener()
        mainPanel.add(nameLabel);
        mainPanel.add(nameText);

        JLabel sexLabel = new JLabel("性别:");
        sexLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        sexLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(sexLabel);
        mainPanel.add(sexText);

        JLabel ageLabel = new JLabel("年龄:");
        ageLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        ageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(ageLabel);
        mainPanel.add(ageText);

        /*JLabel zyLabel = new JLabel("职业:");
        zyLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        zyLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(zyLabel);
        mainPanel.add(zyText);*/

        JLabel telLabel = new JLabel("联系电话:");
        telLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        telLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(telLabel);
        mainPanel.add(telText);

        JLabel keepmoneyLabel = new JLabel("押金:");
        keepmoneyLabel.setFont(new Font("宋体", Font.PLAIN, 25));
        keepmoneyLabel.setHorizontalAlignment(SwingConstants.CENTER);
        mainPanel.add(keepmoneyLabel);
        mainPanel.add(KeepMoneyText);

        JPanel sumPanel = new JPanel();
        sumPanel.setLayout(new FlowLayout());
        JButton submit = new JButton("搜索读者");
        JButton reset = new JButton("更新信息");
        submit.setFont(new Font("宋体", Font.PLAIN, 25));
        reset.setFont(new Font("宋体", Font.PLAIN, 25));
        submit.addActionListener(new submitAction());
        reset.addActionListener(new resetBut());
        sumPanel.add(submit);
        sumPanel.add(reset);
        getContentPane().add(sumPanel, BorderLayout.SOUTH);
    }

    private class idTypeComBoxListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            Reader idType = (Reader) e.getItem();
            reader.setIdtype(idType.getIdtype());
            System.out.println(idType.getIdtype());
        }
    }


    private class submitAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (!ReadId.getText().equals("")) {
                reader = Dao.selectReader(ReadId.getText());
                if (reader != null) {
                    ReadId.setText(reader.getReaderid());
                    idType.setSelectedIndex(reader.getIdtype());
                    IdentityCard.setText(reader.getIdentityCard());
                    nameText.setText(reader.getName());
                    sexText.setText(reader.getSex());
                    ageText.setText(String.valueOf(reader.getAge()));
                    telText.setText(reader.getTel());
                    KeepMoneyText.setText(String.valueOf(reader.getKeepMoney()));
                }

            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输入读者证号");
                return;
            }

        }
    }

    private class resetBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (reader.getIdtype() == 0) {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请选择图书类别");
                return;
            }
            if (!IdentityCard.getText().equals("")) {
                reader.setIdentityCard(IdentityCard.getText());
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输入书名");
                return;
            }
            if (!nameText.getText().equals("")) {
                reader.setName(nameText.getText());
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输作者");
                return;
            }
            if (!sexText.getText().equals("")) {
                reader.setSex(sexText.getText());
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输入译者");
                return;
            }
            if (!ageText.getText().equals("")) {
                reader.setAge(Integer.parseInt(ageText.getText()));
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输入译者");
                return;
            }
            /*if (!zyText.getText().equals("")) {
                reader.setZy(zyText.getText());
            } else {
                JOptionPane.showMessageDialog(ReaderEditFIrame.this, "请输入出版日期");
                return;
            }*/
            if (!telText.getText().equals("")) {
                reader.setTel(telText.getText());
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输入价格");
                return;
            }
            if (!KeepMoneyText.getText().equals("")) {
                reader.setKeepMoney(Double.valueOf(KeepMoneyText.getText()));
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "请输入总数");
                return;
            }


            if (Dao.addNewReader(reader)) {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "添加成功！");
            } else {
                JOptionPane.showMessageDialog(ReaderEditIFrame.this, "添加失败");
            }

        }
    }

    private class IdTypeAction implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            readerTypeModel readerTypeModel = (readerTypeModel) e.getItem();
            reader.setIdtype(readerTypeModel.grade);
        }
    }

    private class readerTypeModel {
        String name;
        int grade;

        public readerTypeModel(String name, int grade) {
            this.name = name;
            this.grade = grade;
        }

        public String toString() {
            return name;
        }
    }

    private class AdminComBoxMode implements ComboBoxModel {
        readerTypeModel[] a = null;
        Object curSelect = null;

        public void setA(readerTypeModel[] a) {
            this.a = a;
        }

        @Override
        public void setSelectedItem(Object anItem) {
            curSelect = anItem;
        }

        @Override
        public Object getSelectedItem() {
            return curSelect;
        }

        @Override
        public int getSize() {
            if (a != null) {
                return a.length;
            }
            return 0;
        }

        @Override
        public Object getElementAt(int index) {
            return a[index];
        }

        @Override
        public void addListDataListener(ListDataListener l) {

        }

        @Override
        public void removeListDataListener(ListDataListener l) {

        }
    }
}

